# This file contains 2 functions from the paper "A Nearly Similar Powerful Test for Mediation", 
# by Kees Jan van Garderen and Noud van Giersbergen, published in the Review of Economics and Statistics.
#
# The first  is the $g_{opt}$ function named gOpt, the second is the $g_{1.2}$ function named g12

#' @param t Maximum (absolute) t-ratio
#' @return Critical value based on varying-g method with tau^*=0.1
#' @example
#' gOpt(2.052)
#' [1] 1.919622
gOpt <- function(t){
  tabs=abs(t)
  x <- c(0.0,0.1,0.11,0.12,0.149,0.16,0.18,0.2,0.22,0.24,0.26,0.28,0.3,0.32,0.34,
         0.36,0.38,0.4,0.42,0.44,0.46,0.48,0.5,0.52,0.54,0.56,0.58,0.6,0.62,0.64,
         0.66,0.68,0.70,0.72,0.74,0.76,0.78,0.8,0.82,0.84,0.86,0.88,0.9,0.92,0.94,
         0.96,0.98,1.0,1.02,1.04,1.06,1.08,1.1,1.12,1.14,1.16,1.18,1.2,1.22,1.24,
         1.26,1.28,1.3,1.32,1.34,1.36,1.38,1.4,1.42,1.44,1.46,1.48,1.5,1.52,1.54,
         1.56,1.58,1.6,1.62,1.64,1.66,1.68,1.7,1.72,1.74,1.76,1.78,1.8,1.82,1.84,
         1.86,1.88,1.9,1.92,1.94,1.96,1.98,2.0,2.02,2.04,2.06,2.08,2.1,2.12,2.14,
         2.16,2.18,2.2,2.22)
  y <- c(0.0,0.1,0.10442420567430982,0.10884823630492242,0.10905928990731939,
         0.11671970433002472,0.1362534932652685,0.1562531362049586,0.17625313620476138,
         0.19625313620438045,0.21625313620398562,0.2362531362035911,0.2562531362031933,
         0.27625313620280345,0.2962531362024044,0.3162531362020066,0.3362531362015954,
         0.35625313620117466,0.3762531362007406,0.3962531362003004,0.4162531361998559,
         0.4362531361994087,0.45625313619894653,0.4762531361984941,0.49625313619802836,
         0.5162531361975771,0.5362531361970972, 0.5562531361966323,0.5762531361961756,
         0.5962531361956576,0.6162531361951945 ,0.6362531361947645,0.6562531361943463,
         0.6762531361939518,0.6962531361935707,0.7162531361932336,0.7362531361928256,
         0.7562531361924566,0.776253136192112 ,0.7962531361917707,0.8162531361914103,
         0.8362531361910628,0.8562531361907504,0.8762531361904211 ,0.8962531361901945,
         0.9162531361899082,0.9362531361896855,0.9562531361895058,0.976253136189357,
         0.9962531361891586,1.016253136188927 ,1.036253136188834 ,1.0562531361886456,
         1.0762531361898517,1.0962531361877554,1.1162531361886663,1.1362531361884074,
         1.1562531361884316,1.1762531361883997,1.1962531361885487,1.2162531361886058,
         1.236249169961608 ,1.2562491699478144,1.2761707540556324,1.2961385967950088,
         1.3116602729399451,1.311660272939983 ,1.3116602729400462,1.3116602729400606,
         1.3116602729401678 ,1.3276222323522948,1.3476222323522515,1.367622232352178,
         1.3876222323519896,1.40762223235168  ,1.4276222323513768,1.447622232351633,
         1.467622232349834 ,1.4876222323487585,1.5076222323502166,1.5276222323504027,
         1.5476222323479396,1.5676222323479245 ,1.5876222323471056,1.607622232345522,
         1.6276222323441385,1.6476222323424914,1.6676222323412837,1.6876222323400614,
         1.7076222323389318,1.7276222323381407,1.7476222323375843,1.7676222323367683,
         1.7876222323354636,1.8076222323344198,1.8276222323330586 ,1.8476222323314195,
         1.8676222323295186,1.8876222323272207,1.907622232325869 ,1.927622232325158,
         1.9476222323250938,1.9599639845400576,1.9599639845400576,1.9599639845400576,
         1.9599639845400576,1.9599639845400576,1.9599639845400576,1.9599639845400576)
  ifelse(tabs>=2.2,1.9599639845400576,approx(x,y,xout=tabs)$y)
}

#' @param t Maximum (absolute) t-ratio
#' @return Critical value based on varying-g method with tau^*=1.217
#' @example
#' g12(2.06)
#' [1] 1.910459
g12 <- function(t){
  tabs=abs(t)  
  x <- c(0.0,1.21699,1.23,1.24,1.25,1.26,1.27,1.28,1.29,1.3,1.31,1.32,1.33,1.34,1.35,
         1.36,1.37,1.38,1.39,1.4,1.41,1.42,1.43,1.44,1.45,1.46,1.47,1.48,1.49,1.5,
         1.51,1.52,1.53,1.54,1.55,1.56,1.57,1.58,1.59,1.6,1.61,1.62,1.63,1.64,1.65,
         1.66,1.67,1.68,1.69,1.7,1.71,1.72,1.73,1.74,1.75,1.76,1.77,1.78,1.79,1.8,
         1.81,1.82,1.83,1.84,1.85,1.86,1.87,1.88,1.89,1.9,1.91,1.92,1.93,1.94,1.95,
         1.96,1.97,1.98,1.99,2.0,2.01,2.02,2.03,2.04,2.05,2.06,2.07,2.08,2.09,2.1,
         2.11,2.12,2.13,2.14,2.15,2.16,2.17,2.18,2.19,2.2,2.21,2.22)
  y <- c(0.0,1.21699,1.21699,1.21699,1.21699,1.21699,1.21699,1.21699,1.21699,
         1.21699,1.21699,1.21699,1.21699,1.21699,1.21699,1.21699,1.220458068388314,
         1.2304580674447265,1.2404580671079568,1.250458067877026,1.2604580693162548,
         1.2704580711826654,1.2804580732870123,1.2904580755205446,1.3004580777947967,
         1.3104580804129289,1.3204580835102997,1.3304580871182918,1.340458091268646,
         1.3504580959934709,1.36045810132509  ,1.3704581072957422,1.3804581139371286,
         1.3904581212797893,1.4004581293522895,1.4104581381801937,1.4204581477848188,
         1.4304581581817966,1.4404581693795326,1.4504581813777324,1.4604581941662653,
         1.4704582077247468,1.4804582220233105,1.49045823702504  ,1.5004582526900512,
         1.5104582689799015,1.5204582858593967,1.5304583032936536,1.5404583212412626,
         1.5504583396434295,1.5604583584405847,1.5704583778059646,1.58045839858615,
         1.5904584189899864,1.6004584551722314,1.610458435779564 ,1.6204585317043485,
         1.6304584676542364,1.6404585723466687,1.6504585439191943,1.660458579487562,
         1.6704586311063618,1.680458614511893 ,1.6904586831971822,1.7004586740350565,
         1.7104587895759948,1.720458779113476 ,1.7304588495464768,1.7404588422583214,
         1.7504589050610244,1.7604589315141315,1.7704589860863458,1.7804590473262458,
         1.7904590619845822,1.8004590712990602,1.81045908387743  ,1.8204590783074086,
         1.8304591078092047,1.8404590865577808,1.8504591164619364,1.860459118772139,
         1.8704591377075586,1.88045913002821  ,1.8904591519234928,1.9004591538036892,
         1.9104591208783905,1.9204592775505347,1.9304590210203234,1.9404592015718336,
         1.9504591491507397,1.9599639845400576,1.9599639845400576,1.9599639845400576,
         1.9599639845400576,1.9599639845400576,1.9599639845400576,1.9599639845400576,
         1.9599639845400576,1.9599639845400576,1.9599639845400576,1.9599639845400576,
         1.9599639845400576)
  ifelse(tabs>=2.2,1.9599639845400576,approx(x,y,xout=tabs)$y)
}
